<?php

// 直接アクセスを拒否する
if (!defined("IN_APP"))
	exit("直接アクセスはできません。");

// ログインしたユーザーのみがアクセスできる
if (!isset($_SESSION['twitter_username']))
	exit("ログインしないとアクセスできません。");

if (@$_POST["action"] == "nickname_update")  {
	$error = false;

	$nickname = $_POST["nickname"];
	$user_id = $_POST["user_id"];
	$group_id = $_SESSION["group"];
	
	$connect = connectToDb();
	
	if (isLeader($connect, $group_id, $_SESSION['user_id'])) {
		$query = "UPDATE group_members ";
		$query.= sprintf("SET member_nickname = '%s' ", mysql_real_escape_string($nickname));
		$query.= sprintf("WHERE user_id = %s AND group_id = %s ",
									mysql_real_escape_string($user_id),
									mysql_real_escape_string($group_id));
		$query.= "AND (del_flag <> 'd' OR del_flag IS NULL)";
		
		try {
			mysql_query($query, $connect);
		}
		catch (Exception $e) {
			$error = true;
			echo "error|" . $MESSAGES['GROUP_NICKNAME_UPDATE_ERROR'];
		}
		
		if (!$error) {
			if ($nickname != "") {
				echo "update|" . $user_id;
			}
			else {
				$query = "SELECT name FROM users ";
				$query.= sprintf("WHERE user_id = %s ", mysql_real_escape_string($user_id));
				$query.= "AND (del_flag <> 'd' OR del_flag IS NULL)";
				
				$result = mysql_query($query, $connect);
				
				$row = mysql_fetch_assoc($result);
				
				$name = $row['name'];

				echo "remove|" . $user_id . "|" . $name;
			}
		}
		
		mysql_close($connect);
	}
	else {
		echo "error|" . $MESSAGES['GROUP_NICKNAME_LEADER_ONLY'];
	}
}
	
	
?>